﻿//------------------------------------------------------------------
// <copyright company="Microsoft">
//     Copyright (c) Microsoft.  All rights reserved.
// </copyright>
//------------------------------------------------------------------
using Dynamic;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using Dynamic.Controller;
using TextBox = Dynamic.Controller.TextBox;
using System.Linq.Expressions;

namespace ReportsApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ToolStrip tsTop = new ToolStrip {Dock = DockStyle.Right};
            tsTop.Items.Add("测试");
            this.reportViewer1.Controls.Add(tsTop);
            this.reportViewer1.PageSettingsChanged += reportViewer1_PageSettingsChanged;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadReport();
        }

        private void reportViewer1_PageSettingsChanged(object sender, EventArgs e)
        {
            var settings = reportViewer1.GetPageSettings();

            LoadReport(settings);
        }

        private void LoadReport(PageSettings settings = null)
        {
            // new一个报表类对象
            DynamicReport dynamicReport;

            if (settings == null)
                dynamicReport = new DynamicReport();
            else
                dynamicReport = new DynamicReport(settings);

            // 设置报表为本地报表
            dynamicReport.SetReportReportViewer(this.reportViewer1);

            

            DataTable data = new DataTable();
            data.Columns.Add("Name", typeof(string));
            data.Columns.Add("School", typeof(string));
            data.Columns.Add("Hometown", typeof(string));
            data.Columns.Add("Hometown1", typeof(string));
            data.Columns.Add("Hometown2", typeof(string));
            data.Columns.Add("Hometown3", typeof(string));
            data.Columns.Add("Hometown4", typeof(string));
            data.Columns.Add("Hometown5", typeof(string));

            data.Rows.Add("Niel", "北京师范", "Anhui","BB","AA","CC","DD","EE");
            //data.Rows.Add("Nacy", "北京师范", "Beijin", "BB", "AA", "CC", "DD", "EE");
            //data.Rows.Add("Perl", "北邮", "Nanjin", "BB", "AA", "CC", "DD", "EE");
            //data.Rows.Add("Lady", "南大", "ShanDong", "BB", "AA", "CC", "DD", "EE");

            //加入数据
            dynamicReport.AddData(data);

            dynamicReport.SetTableNewLine(true);


            // 设置列宽度和对齐方式（可选）
            dynamicReport.SetColoumStyle(new List<ReportColumnStyle>()
            {
                new ReportColumnStyle() {ColumnName = "Name", TextAlign = TextAlign.Center, ColumnWidth = 3/*HeaderVisible = false*/},
                new ReportColumnStyle() {ColumnName = "School", TextAlign = TextAlign.Center, HeaderVisible = true, ColumnWidth = 3},
                new ReportColumnStyle() {ColumnName = "Hometown", TextAlign = TextAlign.Center, HeaderVisible = true, ColumnWidth = 3},
                new ReportColumnStyle() {ColumnName = "Hometown1", TextAlign = TextAlign.Center, HeaderVisible = true, ColumnWidth = 3f},
                new ReportColumnStyle() {ColumnName = "Hometown2", TextAlign = TextAlign.Center, HeaderVisible = true, ColumnWidth = 3},
                new ReportColumnStyle() {ColumnName = "Hometown3", TextAlign = TextAlign.Center, HeaderVisible = true, ColumnWidth = 4},
                new ReportColumnStyle() {ColumnName = "Hometown4", TextAlign = TextAlign.Center, HeaderVisible = true, ColumnWidth = 3},
                new ReportColumnStyle() {ColumnName = "Hometown5", TextAlign = TextAlign.Center, HeaderVisible = true, ColumnWidth = 3},
            });


            // 页眉
            var head = new PageHeader();
            dynamicReport.AddHeader(head);

            //加入标题
            head.AddCaption("This is Caption");

            var rectangle = new Row() { Layout = Dynamic.Controller.Layout.EvenlySplit};
            head.AddRectangle(rectangle);

            var textbox = new TextBox("The first TextBox", textAlign: TextAlign.Left,
               verticalAlign: VerticalAlign.Top);
            var textbox1 = new TextBox("The second TextBox", textAlign: TextAlign.Center, verticalAlign: VerticalAlign.Top)
            {
                Font = new Font("幼圆", 13f, FontStyle.Bold | FontStyle.Italic | FontStyle.Underline),
                Color = Color.Green
            };
            var textbox2 = new TextBox("The Thrid TextBox", textAlign: TextAlign.Right)
            {
                Font = new Font("幼圆", 17f, FontStyle.Bold | FontStyle.Italic),
                Color = Color.Green ,
            };

            rectangle.AddTextBox(textbox);
            rectangle.AddTextBox(textbox1);
            rectangle.AddTextBox(textbox2);

            var rectangleA1 = new Row();
            head.AddRectangle(rectangleA1);
            rectangleA1.AddTextBox(new TextBox("The second rectangle", textAlign: TextAlign.Center,
                verticalAlign: VerticalAlign.Top)
            {
                Font = new Font("黑体", 20f, FontStyle.Bold | FontStyle.Italic | FontStyle.Strikeout),
                Color = Color.Green
            });

           

            var foot = new PageFooter();
            var rectangle2 = new Row();
            var rectangle3 = new Row();
            var textboxA2 = new TextBox("BCD", 1f, TextAlign.Right, VerticalAlign.Bottom);
            rectangle2.AddTextBox(textboxA2);
            rectangle3.AddTextBox(textbox1);
            foot.AddRectangle(rectangle2);
            foot.AddRectangle(rectangle3);
            dynamicReport.AddFooter(foot);
            
            //设置导出报表的名称
            reportViewer1.LocalReport.DisplayName = "测试报表";
            reportViewer1.LocalReport.DisplayName = "BBB";
            //处理报表数据并显示
            dynamicReport.ShowReport();
            this.reportViewer1.RefreshReport();
        }


    }
}